package dabs.engine.plant;

import java.io.Serializable;
import java.util.ArrayList;

import dabs.engine.exceptions.ComponentNotFoundException;
import dabs.engine.messaging.NotificationHandler;

public class PowerPlant implements Serializable {
	private static final long serialVersionUID = 6391416797464333275L;
	
	NotificationHandler poffice;
	
	private ArrayList<ComponentPart> components;
	
	public PowerPlant(NotificationHandler poffice) {
		this.poffice = poffice;
		
		components = new ArrayList<ComponentPart>();
		components.add(new Reactor(poffice, "reactor1", 0, 1000, 10)); 			//0
		components.add(new Reactor(poffice, "reactor2", 0, 1000, 10));			//1
		components.add(new Condenser(poffice, "condenser1", 0, 1000, 20));		//2
		components.add(new Condenser(poffice, "condenser2", 0, 1000, 20));		//3
		components.add(new Pump(poffice, "pump1", 100, 5));						//4
		components.add(new Pump(poffice, "pump2", 100, 5));						//5
		components.add(new Valve(poffice, "valve1", true, 100, 100));			//6
		components.add(new Valve(poffice, "valve2", true, 100, 100));			//7
		components.add(new Valve(poffice, "valve3", true, 100, 100));			//8
		components.add(new Valve(poffice, "valve4", true, 100, 100));			//9
		components.add(new Valve(poffice, "valve5", true, 100, 100));			//10
		components.add(new Valve(poffice, "valve6", true, 100, 100));			//11
		components.add(new Turbine(poffice, "turbine", 1000, 10));				//12
		
		components.get(0).setOutputs(components.get(12));
		components.get(1).setOutputs(components.get(12));
		
		components.get(12).setOutputs(components.get(6));
		components.get(12).setOutputs(components.get(7));
		
		components.get(6).setOutputs(components.get(2));
		components.get(7).setOutputs(components.get(3));
		
		components.get(2).setOutputs(components.get(4));
		components.get(3).setOutputs(components.get(5));
		
		components.get(4).setOutputs(components.get(8));
		components.get(4).setOutputs(components.get(9));
		
		components.get(5).setOutputs(components.get(10));
		components.get(5).setOutputs(components.get(11));
		
		components.get(8).setOutputs(components.get(0));
		components.get(9).setOutputs(components.get(1));
		components.get(10).setOutputs(components.get(0));
		components.get(11).setOutputs(components.get(1));
		
		components.get(0).getNewHydroState().receive(60, true);
		components.get(1).getNewHydroState().receive(60, true);
		components.get(2).getNewHydroState().receive(60, false);
		components.get(3).getNewHydroState().receive(60, false);
	}
	
	public ArrayList<ComponentPart> getComponents() { return components; }
	public NotificationHandler getPostOffice() { return poffice; }
	
	public void tick(){
		for(ComponentPart part : components){
			if (part instanceof Converter)
				((Converter)part).convert();
		}
		for(ComponentPart part : components){
			part.tick();
		}
	}
	
	public void print() {
		for(ComponentPart part : components) {
			part.print();
		}
	}
	
	public ComponentPart getComponent(String name) throws ComponentNotFoundException {
		for(ComponentPart part : components) {
			if (part.getName().equals(name)) 
				return part;
		}
		throw new ComponentNotFoundException(name);
	}

	public String getNameList() {
		String result = "";
		for (ComponentPart part : components) {
			result += part.getName() + " ";
		}
		return result;
	}
	
	public boolean isDead() {
		try {
			if (getComponent("turbine").isDead())
				return true;
			if (getComponent("reactor1").isDead())
				return true;
			if (getComponent("reactor2").isDead())
				return true;
		} catch (ComponentNotFoundException e) {
			System.out.println("Oh lord have mercy on the person who broke the game " +
					"in such a way that we don't have a turbine anymore");
			System.exit(1); // screw you guys, I'm going home
		}
		return false;
	}
	
	// currently we just get the steam that is inputted by the turbine
	public int getScore() {
		try {
			return ((Turbine)getComponent("turbine")).getPowerGenerated();
		} catch (ComponentNotFoundException e) {
			System.out.println(e); // see above for explination
			System.exit(1);
			return 0; // to get rid of java's warnings on "this function does not return an int"
		}
	}
}
